﻿using Microsoft.Extensions.DependencyInjection;

namespace IOC
{
    internal class Program
    {
        static void Main(string[] args)
        {
            ServiceCollection services = new ServiceCollection();
            //需要研究
            services.AddScoped<ITestService, TestServiceImpl>();
            services.AddScoped<ITestService, TestServiceImpl2>();
            //services.AddScoped(typeof(ITestService), typeof(TestServiceImpl));
            //services.AddSingleton<ITestService, TestServiceImpl>();
            //services.AddSingleton(typeof(ITestService), new TestServiceImpl());
            using (ServiceProvider sp = services.BuildServiceProvider())
            {
                //GetService如果找不到服务，就返回null
                //ITestService t = sp.GetService<ITestService>();
                //GetRequiredService必须的，如果找不到，就抛异常
                //ITestService t = sp.GetRequiredService<ITestService>();
                //t.Name = "tom";
                //t.SayHi();
                //Console.WriteLine(t.GetType());
                //加载过个
                IEnumerable<ITestService> t1 = sp.GetServices<ITestService>();
                foreach (ITestService e in t1)
                {
                    Console.WriteLine(e.GetType());
                }
            }
        }

        static void Main1(string[] args)
        {
            ServiceCollection services = new ServiceCollection();
            //新建不同的对象
            //services.AddTransient<TestServiceImpl>();
            //单例
            //services.AddSingleton<TestServiceImpl>();
            //范围
            services.AddScoped<TestServiceImpl>();
            using (ServiceProvider serviceProvider = services.BuildServiceProvider())
            {
                /*TestServiceImpl testService = serviceProvider.GetService<TestServiceImpl>();
                testService.Name = "tom";
                testService.SayHi();

                TestServiceImpl testService1 = serviceProvider.GetService<TestServiceImpl>();

                Console.WriteLine(object.ReferenceEquals(testService, testService1));*/
                //范围,同一个对象，出了范围对象不同
                using (IServiceScope scope1 = serviceProvider.CreateScope())
                {
                    TestServiceImpl testService2 = scope1.ServiceProvider.GetService<TestServiceImpl>();
                    testService2.Name = "tom";
                    testService2.SayHi();

                    TestServiceImpl testService3 = scope1.ServiceProvider.GetService<TestServiceImpl>();

                    Console.WriteLine(object.ReferenceEquals(testService2, testService3));
                }

                using (IServiceScope scope2 = serviceProvider.CreateScope())
                {
                    TestServiceImpl testService2 = scope2.ServiceProvider.GetService<TestServiceImpl>();
                    testService2.Name = "tom";
                    testService2.SayHi();

                    TestServiceImpl testService3 = scope2.ServiceProvider.GetService<TestServiceImpl>();

                    Console.WriteLine(object.ReferenceEquals(testService2, testService3));
                }
            }

            ITestService t = new TestServiceImpl();
            t.Name = "Test";
            t.SayHi();
        }
    }

    public interface ITestService
    {
        public string Name { get; set; }
        public void SayHi();
    }

    public class TestServiceImpl : ITestService
    {
        public string Name { get; set; }

        public void SayHi()
        {
            Console.WriteLine($"Hi, I'm {Name}");
        }
    }
    public class TestServiceImpl2 : ITestService
    {
        public string Name { get; set; }

        public void SayHi()
        {
            Console.WriteLine($"你好，我是{Name}");
        }
    }
}